home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / forms / searchst / findform.frm (.txt) next >
Encoding:
Visual Basic Form  |  1994-11-22  |  8.1 KB  |  237 lines

  1. VERSION 2.00
  2. Begin Form FindForm 
  3.    BackColor       =   &H8000000F&
  4.    BorderStyle     =   3  'Fixed Double
  5.    Caption         =   "Form1"
  6.    ClientHeight    =   1995
  7.    ClientLeft      =   2625
  8.    ClientTop       =   4110
  9.    ClientWidth     =   5850
  10.    ControlBox      =   0   'False
  11.    Height          =   2400
  12.    Icon            =   FINDFORM.FRX:0000
  13.    Left            =   2565
  14.    LinkTopic       =   "Form1"
  15.    MaxButton       =   0   'False
  16.    MinButton       =   0   'False
  17.    ScaleHeight     =   1995
  18.    ScaleWidth      =   5850
  19.    Tag             =   "FindFormTag"
  20.    Top             =   3765
  21.    Width           =   5970
  22.    Begin CheckBox MatchCase 
  23.       BackColor       =   &H00C0C0C0&
  24.       Caption         =   "MatchCase"
  25.       Height          =   255
  26.       Left            =   120
  27.       TabIndex        =   8
  28.       Top             =   1680
  29.       Width           =   1695
  30.    End
  31.    Begin CommandButton Action 
  32.       Caption         =   "Verify"
  33.       FontBold        =   0   'False
  34.       FontItalic      =   0   'False
  35.       FontName        =   "MS Sans Serif"
  36.       FontSize        =   8.25
  37.       FontStrikethru  =   0   'False
  38.       FontUnderline   =   0   'False
  39.       Height          =   375
  40.       Index           =   1
  41.       Left            =   4560
  42.       TabIndex        =   7
  43.       Top             =   600
  44.       Width           =   1215
  45.    End
  46.    Begin CommandButton Action 
  47.       Caption         =   "Replace"
  48.       FontBold        =   0   'False
  49.       FontItalic      =   0   'False
  50.       FontName        =   "MS Sans Serif"
  51.       FontSize        =   8.25
  52.       FontStrikethru  =   0   'False
  53.       FontUnderline   =   0   'False
  54.       Height          =   375
  55.       Index           =   2
  56.       Left            =   4560
  57.       TabIndex        =   6
  58.       Top             =   1080
  59.       Width           =   1215
  60.    End
  61.    Begin CommandButton Action 
  62.       Caption         =   "Cancel"
  63.       FontBold        =   0   'False
  64.       FontItalic      =   0   'False
  65.       FontName        =   "MS Sans Serif"
  66.       FontSize        =   8.25
  67.       FontStrikethru  =   0   'False
  68.       FontUnderline   =   0   'False
  69.       Height          =   375
  70.       Index           =   3
  71.       Left            =   4560
  72.       TabIndex        =   5
  73.       Top             =   1560
  74.       Width           =   1215
  75.    End
  76.    Begin CommandButton Action 
  77.       Caption         =   "Find"
  78.       FontBold        =   0   'False
  79.       FontItalic      =   0   'False
  80.       FontName        =   "MS Sans Serif"
  81.       FontSize        =   8.25
  82.       FontStrikethru  =   0   'False
  83.       FontUnderline   =   0   'False
  84.       Height          =   375
  85.       Index           =   0
  86.       Left            =   4560
  87.       TabIndex        =   4
  88.       Top             =   120
  89.       Width           =   1215
  90.    End
  91.    Begin TextBox ReplaceText 
  92.       Height          =   285
  93.       Left            =   1560
  94.       TabIndex        =   3
  95.       Top             =   600
  96.       Width           =   2775
  97.    End
  98.    Begin TextBox FindText 
  99.       Height          =   285
  100.       Left            =   1560
  101.       TabIndex        =   2
  102.       Top             =   120
  103.       Width           =   2775
  104.    End
  105.    Begin Label Label2 
  106.       Alignment       =   1  'Right Justify
  107.       BackColor       =   &H00C0C0C0&
  108.       Caption         =   "Replace With:"
  109.       Height          =   255
  110.       Left            =   120
  111.       TabIndex        =   1
  112.       Top             =   600
  113.       Width           =   1335
  114.    End
  115.    Begin Label Label1 
  116.       Alignment       =   1  'Right Justify
  117.       BackColor       =   &H00C0C0C0&
  118.       Caption         =   "Search For:"
  119.       Height          =   255
  120.       Left            =   120
  121.       TabIndex        =   0
  122.       Top             =   120
  123.       Width           =   1335
  124.    End
  125. Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
  126. Const AppTitle = "Steve's Find and Replace"
  127. Const SWP_SHOWWINDOW = &H40
  128. Const HWND_TOPMOST = -1
  129. Sub Action_Click (Index As Integer)
  130. Dim SearchType As Integer
  131. Dim StartPos As Integer
  132. Dim NextPos As Integer
  133. Dim FindVal As String
  134. Dim ReplaceVal As String
  135. Dim SearchLen As Integer
  136. Dim CaseSensitive As Integer
  137. 'search constants
  138. Const SRCH_FIND = 0
  139. Const SRCH_VERIFY = 1
  140. Const SRCH_REPLACE = 2
  141. 'message box constants
  142. Const MB_QUESTION = 36
  143. Const MB_YES = 6
  144. 'if the user selected cancel
  145. If Index = 3 Then
  146.   'unload the form
  147.   Unload Me
  148.   'quit the subroutine
  149.   Exit Sub
  150.   'otherwise, define the type of Find and Replace
  151.   SearchType = Index
  152. End If
  153. 'make sure text is entered
  154. If FindText.Text = "" Then
  155.   MsgBox "The Search For field must contain one or more characters to look for.", 16, ProgTitle
  156.   FindText.SetFocus
  157.   Exit Sub
  158. End If
  159. 'change pointer to an hourglass
  160. Screen.MousePointer = 11
  161. 'set initial values
  162. NextPos = 0    'the next position we start looking for a match
  163. FindVal = FindText.Text  'the text we're looking for
  164. ReplaceVal = ReplaceText.Text  'the text we want to replace with
  165. SearchLen = Len(FindVal)  'the length of the text we're looking for
  166. 'the starting position we look for a match
  167. If (FAndRControl.SelText = ReplaceVal) Or (SearchType = SRCH_FIND) Then
  168.   'if we've already done this one or we're just finding, we want to go
  169.   'past it to the next one
  170.   StartPos = FAndRControl.SelStart + 2
  171.   'SelStart is 0-based, so we add one to make it one-based (like our text)
  172.   StartPos = FAndRControl.SelStart + 1
  173. End If
  174. 'if we haven't selected any text yet, we have to start at position 1
  175. If StartPos = 2 Then StartPos = 1
  176. 'hide the form so it won't get in the way of the dialog boxes
  177. Me.Visible = False
  178. 'read the check box to see if the search is case sensitive
  179. If MatchCase.Value = 0 Then
  180.   CaseSensitive = 1
  181.   CaseSensitive = 0
  182. End If
  183. 'look for the first occurrence of our string
  184. NextPos = InStr(StartPos, (FAndRControl.Text), FindVal, CaseSensitive)
  185. 'while we find occurences of our string
  186. While NextPos > 0
  187.   'set the value for the next position we'll start looking at
  188.   StartPos = NextPos + 1
  189.   'select the text in the text box
  190.   FAndRControl.SelStart = NextPos - 1
  191.   FAndRControl.SelLength = SearchLen
  192.   'depending on the type of search we're doing
  193.   Select Case SearchType
  194.     'a search and replace, replace the text and keep on going
  195.     Case SRCH_REPLACE
  196.       FAndRControl.SelText = ReplaceVal
  197.     'a verify, if user says to replace, then replace
  198.     Case SRCH_VERIFY
  199.       'move the focus to the text box
  200.       FAndRControl.SetFocus
  201.       If MsgBox("Replace Text?", MB_QUESTION, AppTitle) = MB_YES Then FAndRControl.SelText = ReplaceVal
  202.     'user just wants to find next value
  203.     Case SRCH_FIND
  204.       'move the search starting position to the end of the text so no more matches will
  205.       'be found; it forces the user to keep hitting Find and move on
  206.       StartPos = Len(FAndRControl.Text)
  207.   End Select
  208.   'look for the next occurence of our string
  209.   NextPos = InStr(StartPos, (FAndRControl.Text), FindVal, CaseSensitive)
  210. 'let the user know we've finished replacing
  211. If SearchType <> SRCH_FIND Then MsgBox "Search completed.", 48, AppTitle
  212. 'reset the form's visible property so the user can knows what's going on
  213. Me.Visible = True
  214. 'set the focus on the text box
  215. FAndRControl.SetFocus
  216. 'change pointer back to normal
  217. Screen.MousePointer = 0
  218. End Sub
  219. Sub CenterForm (SForm As Form)
  220. SForm.Top = (Screen.Height - SForm.Height) / 2
  221. SForm.Left = (Screen.Width - SForm.Width) / 2
  222. End Sub
  223. Sub Form_Load ()
  224. 'set form caption
  225. Me.Caption = AppTitle
  226. 'center the form
  227. CenterForm Me
  228. 'set the form tag
  229. Me.Tag = FIND_FORM_TAG
  230. 'make the form a top-most window
  231. SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW
  232. 'if text in the control is selected, make it the default search for text
  233. If FAndRControl.SelText <> "" Then FindText.Text = Trim$(FAndRControl.SelText)
  234. 'if there's text in the findtext, select it
  235. FindText.SelLength = Len(FindText)
  236. End Sub
  237.